<!doctype html>
<html lang="en">
<head>
    <title>Code coverage report for rpc/core/server/HeroServer.js</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="../../../prettify.css" />
    <link rel="stylesheet" href="../../../base.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type='text/css'>
        .coverage-summary .sorter {
            background-image: url(../../../sort-arrow-sprite.png);
        }
    </style>
</head>
<body>
<div class='wrapper'>
  <div class='pad1'>
    <h1>
      <a href="../../../index.html">all files</a> / <a href="index.html">rpc/core/server/</a> HeroServer.js
    </h1>
    <div class='clearfix'>
      <div class='fl pad1y space-right2'>
        <span class="strong">55.17% </span>
        <span class="quiet">Statements</span>
        <span class='fraction'>64/116</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">33.33% </span>
        <span class="quiet">Branches</span>
        <span class='fraction'>10/30</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">42.11% </span>
        <span class="quiet">Functions</span>
        <span class='fraction'>8/19</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">56.14% </span>
        <span class="quiet">Lines</span>
        <span class='fraction'>64/114</span>
      </div>
    </div>
  </div>
  <div class='status-line medium'></div>
<pre><table class="coverage">
<tr><td class="line-count quiet">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182</td><td class="line-coverage quiet"><span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-yes">6×</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1×</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">var TarsProtocol  = require("../rpc-tars").server;
var Endpoint     = require("@tars/utils").Endpoint;
var Configure    = require("@tars/utils").Config;
var TarsMonitor   = require("@tars/monitor");
var Communicator = require("../client/Communicator.js").Communicator;
var BindAdapter  = require("./BindAdapter.js").BindAdapter;
&nbsp;
///////////////////////////////////////////////封装标准tars服务//////////////////////////////////////////////////////////
var HeroServer = function () {
    this._adapters       = [];          //当前服务上的所有绑定端口
    this._servants       = {};          //当前服务上的所有绑定端口对应的处理类
    this._configure      = undefined;
    this._configureFile  = "";
    this._communicator   = undefined;   //框架已经初始化的通信器
    this.Application     = "";
    this.ServerName      = "";
};
module.exports.HeroServer = HeroServer;
&nbsp;
//创建一个BindAdapter的参数
HeroServer.BindAdapterOption = function () {
    this.endpoint     = "";         //必选。当前BindAdapter的监听地址，比如tcp -h 127.0.0.1 -p 14002 -t 10000
    this.name         = "";         //可选。当前BindAdapter的名字
    this.servant      = "";         //可选。当前BindAdapter上的Servant的名字
    this.maxconns     = 1024;       //可选。当前BindAdapter上可以支持的最大链接数
    this.protocolName = "tars";      //可选。当前BindAdapter上的协议解析器的名字
    this.protocol     = undefined;  //可选。当前BindAdapter上的协议解析器，如果不设置或者为"tars"则默认为tars协议
    this.handleImp    = undefined;  //必选。当前BindAdapter上的请求处理类
};
&nbsp;
//定义访问属性
HeroServer.prototype.__defineGetter__("communicator", <span class="fstat-no" title="function not covered" >function () {</span> <span class="cstat-no" title="statement not covered" >return this._communicator;}</span>);
&nbsp;
HeroServer.prototype.__defineGetter__("configure", <span class="fstat-no" title="function not covered" >function () {</span> <span class="cstat-no" title="statement not covered" >return this._configure; </span>});
&nbsp;
//使用配置文件初始化服务
HeroServer.prototype.initialize = <span class="fstat-no" title="function not covered" >function ($sConfigFile, $initFunction) {</span>
<span class="cstat-no" title="statement not covered" >    this._configureFile = $sConfigFile;</span>
<span class="cstat-no" title="statement not covered" >    this._configure     = new Configure();</span>
<span class="cstat-no" title="statement not covered" >    this._configure.parseFile(this._configureFile);</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    this._initializeClient(this._configureFile);</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    this._initializeServer();</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    TarsMonitor.stat.init(this._configure.json);</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    $initFunction &amp;&amp; $initFunction(this);</span>
};
&nbsp;
//为服务Servant增加处理类
HeroServer.prototype.addServant = <span class="fstat-no" title="function not covered" >function ($ServantHandle, $ServantName) {</span>
<span class="cstat-no" title="statement not covered" >    this._servants[$ServantName] = $ServantHandle;</span>
};
&nbsp;
//启动服务
HeroServer.prototype.start = function (option) { //option的类型为：BindAdapterOption
    option &amp;&amp; this._addBindAdapter(option);
    return this._waitForShutdown();
};
&nbsp;
//停止服务
HeroServer.prototype.stop = function () {
    this._communicator &amp;&amp; <span class="branch-1 cbranch-no" title="branch not covered" >this._communicator.destroy();</span> //如果有初始化通信器，则把通信器关闭
&nbsp;
    for (var i = 0; i &lt; this._adapters.length; i++) {
        this._adapters[i].stop();
    }
    this._adapters.splice(0, this._adapters.length);
};
&nbsp;
HeroServer.prototype._initializeClient = <span class="fstat-no" title="function not covered" >function ($sConfigFile) {</span>
<span class="cstat-no" title="statement not covered" >    this._communicator = Communicator.New();</span>
<span class="cstat-no" title="statement not covered" >    this._communicator.initialize($sConfigFile);</span>
};
&nbsp;
HeroServer.prototype._initializeServer = <span class="fstat-no" title="function not covered" >function () {</span>
<span class="cstat-no" title="statement not covered" >    this.Application = this._configure.get("tars.application.server.app");</span>
<span class="cstat-no" title="statement not covered" >    this.ServerName  = this._configure.get("tars.application.server.server");</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    var svrList = this._configure.getDomain("tars.application.server");</span>
<span class="cstat-no" title="statement not covered" >    for (var i = 0, len = svrList.length; i &lt; len; i++) {</span>
<span class="cstat-no" title="statement not covered" >        var config = this._configure.get("tars.application.server.&lt;" + svrList[i] + "&gt;");</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >        var option = new HeroServer.BindAdapterOption();</span>
<span class="cstat-no" title="statement not covered" >        option.name          = svrList[i];</span>
<span class="cstat-no" title="statement not covered" >        option.servant       = config.servant;</span>
<span class="cstat-no" title="statement not covered" >        option.endpoint      = config.endpoint;</span>
<span class="cstat-no" title="statement not covered" >        option.maxconns      = config.maxconns;</span>
<span class="cstat-no" title="statement not covered" >        option.protocolName  = config.protocol;</span>
<span class="cstat-no" title="statement not covered" >        option.protocol      = TarsProtocol;</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >        this._addBindAdapter(option);</span>
    }
};
&nbsp;
//向服务中注册BindAdapter
HeroServer.prototype._addBindAdapter = function ($options) {
    var adapter = new BindAdapter();
    adapter.name          = $options.name;
    adapter.servantName   = $options.servant;
    adapter.endpoint      = Endpoint.parse($options.endpoint);
    adapter.maxconns      = $options.maxconns?$options.maxconns:<span class="branch-1 cbranch-no" title="branch not covered" >1024;</span>
    adapter.protocolName  = $options.protocolName;
    adapter.protocol      = adapter.isTarsProtocol()?TarsProtocol:$options.protocol;
&nbsp;
    this._servants[adapter.servantName] = $options.handleImp;
    this._adapters.push(adapter);
};
&nbsp;
HeroServer.prototype._waitForShutdown = function () {
    var initializePromiseArray = [];
    for (var i = 0, len = this._adapters.length; i &lt; len; i++) {
        //判断handleImp的initialize方法是否返回promise对象，支持server启动前的准备工作
        (function (){
            var adapter = this._adapters[i];
            adapter.handleImp = new this._servants[adapter.servantName]();
            adapter.handleImp.application = this;
            var promise = adapter.handleImp.initialize();
            <span class="missing-if-branch" title="if path not taken" >I</span>if(promise &amp;&amp; <span class="branch-1 cbranch-no" title="branch not covered" >typeof promise.then == "function")</span>{
<span class="cstat-no" title="statement not covered" >                initializePromiseArray.push(promise.then(<span class="fstat-no" title="function not covered" >function (data) {</span></span>
<span class="cstat-no" title="statement not covered" >                    adapter.start();</span>
<span class="cstat-no" title="statement not covered" >                    return data;</span>
                }));
            } else {
                adapter.start();
            }
        }).call(this);
    }
    <span class="missing-if-branch" title="if path not taken" >I</span>if(initializePromiseArray.length &gt; 0){
<span class="cstat-no" title="statement not covered" >        return Promise.all(initializePromiseArray);</span>
    }
};
&nbsp;
///////////////////////////////////////////////封装简单tars服务//////////////////////////////////////////////////////////
var SimpleServer = <span class="fstat-no" title="function not covered" >function ($HandleImp) {</span>
<span class="cstat-no" title="statement not covered" >    this._server    = new HeroServer();</span>
<span class="cstat-no" title="statement not covered" >    this._handleImp = $HandleImp;</span>
};
&nbsp;
SimpleServer.prototype.start = <span class="fstat-no" title="function not covered" >function ($options) {</span>
<span class="cstat-no" title="statement not covered" >    var svroption = new HeroServer.BindAdapterOption();</span>
<span class="cstat-no" title="statement not covered" >    svroption.endpoint      = $options.endpoint;</span>
<span class="cstat-no" title="statement not covered" >    svroption.servant       = $options.servant;</span>
<span class="cstat-no" title="statement not covered" >    svroption.maxconns      = $options.maxconns;</span>
<span class="cstat-no" title="statement not covered" >    svroption.protocolName  = $options.protocol;</span>
<span class="cstat-no" title="statement not covered" >    svroption.handleImp     = $options.handleImp?$options.handleImp:this._handleImp;</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    process.env.TARS_CONFIG &amp;&amp; this._server._initializeClient(process.env.TARS_CONFIG);</span>
<span class="cstat-no" title="statement not covered" >    return this._server.start(svroption);</span>
};
&nbsp;
SimpleServer.prototype.stop = <span class="fstat-no" title="function not covered" >function () {</span>
<span class="cstat-no" title="statement not covered" >    this._server &amp;&amp; this._server.stop();</span>
};
&nbsp;
///////////////////////////////////////////////对外提供的静态方法/////////////////////////////////////////////////////////
HeroServer.createServer = function ($HandleImp) {
    return $HandleImp?(<span class="branch-0 cbranch-no" title="branch not covered" >new SimpleServer($HandleImp))</span>:(new HeroServer());
};
&nbsp;
HeroServer.getServant = <span class="fstat-no" title="function not covered" >function ($sConfigFile, $ServantName) {</span>
<span class="cstat-no" title="statement not covered" >    var configure = new Configure();</span>
<span class="cstat-no" title="statement not covered" >    configure.parseFile($sConfigFile);</span>
&nbsp;
<span class="cstat-no" title="statement not covered" >    var svrList  = configure.getDomain("tars.application.server");</span>
<span class="cstat-no" title="statement not covered" >    var servants = [];</span>
<span class="cstat-no" title="statement not covered" >    for (var i = 0, len = svrList.length; i &lt; len; i++) {</span>
<span class="cstat-no" title="statement not covered" >        var config = configure.get("tars.application.server.&lt;" + svrList[i] + "&gt;");</span>
<span class="cstat-no" title="statement not covered" >        if ($ServantName &amp;&amp; config.servant === $ServantName) {</span>
<span class="cstat-no" title="statement not covered" >            servants.push(config);</span>
<span class="cstat-no" title="statement not covered" >            break;</span>
        }
&nbsp;
<span class="cstat-no" title="statement not covered" >        if ($ServantName === undefined) {</span>
<span class="cstat-no" title="statement not covered" >            servants.push(config);</span>
        }
    }
&nbsp;
<span class="cstat-no" title="statement not covered" >    return servants;</span>
};
&nbsp;</pre></td></tr>
</table></pre>
<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
  Code coverage
  generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Tue Jun 30 2020 12:15:32 GMT+0800 (GMT+08:00)
</div>
</div>
<script src="../../../prettify.js"></script>
<script>
window.onload = function () {
        if (typeof prettyPrint === 'function') {
            prettyPrint();
        }
};
</script>
<script src="../../../sorter.js"></script>
</body>
</html>
